package com.persondemo.videoappliction.ui.search.presenter;

import android.text.TextUtils;
import android.util.Log;

import com.persondemo.videoappliction.bean.SearchBean;
import com.persondemo.videoappliction.bean.VideoBean;
import com.persondemo.videoappliction.bean.WaiVideBean;
import com.persondemo.videoappliction.net.BaseApi;
import com.persondemo.videoappliction.net.RxSchedulers;
import com.persondemo.videoappliction.ui.base.BaseContract;
import com.persondemo.videoappliction.ui.base.BasePresenter;
import com.persondemo.videoappliction.ui.search.contract.SearchContract;
import com.persondemo.videoappliction.utils.T;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2018/3/21.
 */

public class SearchPresenter extends BasePresenter<SearchContract.View> implements SearchContract.Presenter {
    BaseApi baseApi;

    @Inject
    public SearchPresenter(BaseApi baseApi) {
        this.baseApi = baseApi;
    }

    @Override
    public void getserachdata(String title) {

        if (TextUtils.isEmpty(title)) {
            mView.titleError();
            return;
        }
        baseApi.getserach(title)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(new Observer<SearchBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull SearchBean searchBean) {
                        if (searchBean == null) {
                            mView.showFaild();
                            return;
                        } else {
                            mView.loaddata(searchBean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.showFaild();
                        Log.e("lidaming",e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void getwaidata(String url) {
        if (TextUtils.isEmpty(url)) {
            mView.showFaild();
            return;
        } else {
            baseApi.getwaidata(url)
                    .compose(RxSchedulers.applySchedulers())
                    .compose(mView.bindToLife())
                    .subscribe(new Observer<WaiVideBean>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable d) {

                        }

                        @Override
                        public void onNext(@NonNull WaiVideBean waiVideBean) {
                            Log.i("lidaming02", waiVideBean.getData() + "");
                            if (waiVideBean == null) {
                                mView.showFaild();
                                return;
                            } else {

                                mView.gotoVideodetils(waiVideBean.getData());
                            }
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            Log.e("lidaming",e.toString());
                            mView.showFaild();
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

}
